﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.WebApp
{
    public partial class WebmasterRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            MultiViewSeeForm.ActiveViewIndex = WebmasterManager.IsWebmaster(User.Identity.Name) ? 1 : 0;
            if (MultiViewSeeForm.ActiveViewIndex==0 && WebmasterRequestManager.HavePendingRequest(User.Identity.Name))
            {
               Model.WebmasterRequest webmasterReq = WebmasterRequestDataAccess.GetWebmasterRequestByCandidateId(
                    UserDataAccess.GetUserByEmail(User.Identity.Name).Id);
                lblPositives.Text = webmasterReq.PositiveVotes + " positives";
                lblNegatives.Text = webmasterReq.NegativeVotes + " negatives";
                MultiViewSeeForm.ActiveViewIndex = 2;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                lblMessage.Visible = false;
                if (!chkTermsAndConditions.Checked)
                    throw new BusinessException("You have to read and accept the terms and conditions!");
                WebmasterRequestManager.RegisterNewRequest(User.Identity.Name, txtSkills.Text,
                                                           chkWantReplyTickets.Checked, chkWantSetWebmasters.Checked);
                Response.Write("<script>alert('Request sent successfully, you will be notified if you has been choosed!') ;window.location.href = 'Default.aspx'; </script>");
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
                lblMessage.Visible = true;
            }
        }
    }
}